<?php
$frist=!count($_POST);
?>
<html>
    <body>
        <h1>添加用户</h1>
        <!DOCTYPE html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if($frist){
        ?>
        <form action="action.php?id=add" method="post"><!--url后面加上id便于action.php判断是哪个页面跳转过来的-->
           账户:
            <input type="text" name="name"> <br/>
           密码:
           <input type="password" name="passwd"><br/>

            性别:
            <input type="radio" name="sex" value="男">男
            <input type="radio" name="sex" value="女">女
            <br/>
            年龄：
             <select name="age">
                <?php
                for($index=18;$index<=100;$index++){
                    echo "<option>$index</option>";
                }
                ?>
            </select>
            <br/>
            <input type="file"  name="pic">
            <br/>
            <input type="submit" value="上传">
            <input type="reset" value="取消">
        </form>
        
        <?php
        }else{
         header('Content-type:text/html;charset=utf-8');//设置编码格式
        // put your code here
            $hostname="127.0.0.1";
            $username="root";
            $password="123456";
            $database="add";
            $link=mysqli_connect($hostname, $username, $password, $database); //连接数据库
           
            $name=$_POST["name"];
            $passwd=$_POST["passwd"];
            $sex=$_POST["sex"];
            $age=$_POST["age"];
            $pic=$_POST["pic"];
//            $sqlstring="insert into user(username,passwd,pic,sex,age)"
//                    . " values('${name}','${passwd}','${pic}','${sex}',${age})";
            $sqlstring="INSERT INTO `add`.`user` (`username`, `passwd`, `age`, `sex`, `pic`)"
                    . " VALUES ('${name}', '${passwd}', '${age}', '${sex}', '${pic}');";
            $result=mysqli_query($link,$sqlstring);
              if($result){
                echo  "<script>;"
                . "location='index.php';</script>";
            }else{
                echo "添加失败";
            }
             if(!$link){
                echo"连接失败";
            }
        }
           
        
        ?>
    </body>
</html>
